 //
//  BaseViewController.m
//
//
//  Created by Fidetro on 16/9/22.
//  Copyright © 2016年 Fidetro. All rights reserved.
//

#import "BaseViewController.h"



@interface BaseViewController ()

@end

@implementation BaseViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //顶部不留空
    self.automaticallyAdjustsScrollViewInsets = NO;
    //取消半透明
    self.navigationController.navigationBar.translucent = NO;
    //设置背景颜色
    self.view.backgroundColor = [UIColor colorWithHexString:@"f8f7f7"];
    //设置导航栏图片
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bg"] forBarMetrics:UIBarMetricsDefault];
    // 开启右滑返回
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    [self masLayoutSubview];
    [self setNavigationStyle];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}


- (void)hideNavigationBottomLine
{
    self.navigationController.navigationBar.shadowImage = [UIImage new];
}

- (void)hideNavigationBar
{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new]
                                                  forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    
    self.navigationController.navigationBar.translucent = YES;
}

/**
 状态栏变白 要设置info.plist View controller-based status bar appearance  为NO
 */
-(UIStatusBarStyle)preferredStatusBarStyle{
    
    return UIStatusBarStyleDefault;
    
}


#pragma mark - 设置NavigationTitle
- (void)setNavigationTitle:(NSString *)title titleColor:(UIColor *)titleColor{
    
    self.navigationItem.title = title;
    if (titleColor == nil) {
        
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#dab866"],
                                                                        NSFontAttributeName:[UIFont systemFontOfSize:17.f]};
    }else{
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:titleColor,
                                                                        NSFontAttributeName:[UIFont systemFontOfSize:17.f]};
    }
 
    
}

- (void)setCentImage:(UIImage *)image{
    
    self.navigationItem.titleView = [[UIImageView alloc]initWithImage:image];
}

#pragma mark - 左侧按钮设置
- (void)setLeftButtonTitle:(NSString *)title{
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.leftButton];
    [self.leftButton setTitle:title forState:UIControlStateNormal];
    [self.leftButton setImage:[UIImage imageNamed:@"icon_back_nor"] forState:UIControlStateNormal];
    [self.leftButton layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:3];
    [self.leftButton setFrame:CGRectMake(0, 0, 50, 30)];
}

- (void)setLeftButtonImage:(UIImage *)image{
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.leftButton];
    [self.leftButton setImage:image forState:UIControlStateNormal];
    [self.leftButton setFrame:CGRectMake(0, 0, 25,image.size.height)];
    
    
    
}
#pragma mark - 右侧按钮设置
- (void)setRightButtonTitle:(NSString *)title
{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
    [self.rightButton setTitleColor:[UIColor colorWithHexString:@"#dab866"] forState:UIControlStateNormal];
    [self.rightButton.titleLabel setFont:[UIFont systemFontOfSize:14.0f]];
    [self.rightButton setTitle:title forState:UIControlStateNormal];
    [self.rightButton setFrame:CGRectMake(0, 0, BASE_TEXTSIZE(title, self.rightButton.titleLabel.font).width, BASE_TEXTSIZE(title,self.rightButton.titleLabel.font).height)];
}

- (void)setRightButtonImage:(UIImage *)image
{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
    [self.rightButton setImage:image forState:UIControlStateNormal];
    [self.rightButton setFrame:CGRectMake(0, 0, image.size.width,image.size.height)];
}
#pragma mark - --------------------------touch Event--------------------------
- (void)onLeftButtonClick:(id)sender
{
    if ([self.navigationController.visibleViewController isMemberOfClass:[UITabBarController class]]) {
        return;
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)onRightButtonClick:(id)sender
{
    
}

-(void)touchesEnded:(NSSet<UITouch*> *)touches withEvent:(UIEvent*)event
{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];//点击屏幕收起键盘
}
#pragma mark - --------------------------get方法--------------------------
- (UIButton *)leftButton
{
    if (!_leftButton) {
        _leftButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
        [_leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_leftButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
        [_leftButton setExclusiveTouch:YES];
        [_leftButton.imageView setContentMode:UIViewContentModeScaleAspectFit];
        
        [_leftButton addTarget:self action:@selector(onLeftButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _leftButton;
}

- (UIButton *)rightButton
{
    if (!_rightButton) {
        _rightButton = [[UIButton alloc]init];
        [_rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_rightButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
        [_rightButton setExclusiveTouch:YES];
        [_rightButton.imageView setContentMode:UIViewContentModeScaleAspectFit];
        
        [_rightButton addTarget:self action:@selector(onRightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _rightButton;
}


- (void)masLayoutSubview
{
    
}

- (void)setNavigationStyle
{
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
